﻿#include "ItemRentDialog.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../Settings.h"
#include "../../MirSounds/SoundManager.h"
#include "../GameScene.h"
#include "../../MirNetwork/Network.h"
#include "../../../Shared/ClientPackets.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirNetwork;
using namespace Client::MirSounds;
namespace C = ClientPackets;

namespace Client::MirScenes::Dialogs
{

    ItemRentDialog::ItemRentDialog() : _lockButton(new MirButton();
        _lockButton->setIndex(250);
        _lockButton->setHoverIndex(251);
        Point tempVar(22, 76);
        _lockButton->setLocation(&tempVar);
        Size tempVar2(28, 25);
        _lockButton->setSize(&tempVar2);
        _lockButton->setLibrary(Libraries::Prguse);
        _lockButton->setParent(this);
        _lockButton->setPressedIndex(252);
        _lockButton->setSound(SoundList::ButtonA))
        {
        setIndex(238);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        Size tempVar3(204, 109);
        setSize(&tempVar3);
        Point tempVar4(Settings::ScreenWidth - getSize()->Width - getSize()->Width / 2, getSize()->Height + getSize()->Height / 2);
        setLocation(&tempVar4);
        setSort(true);

        auto closeButton = new MirButton();
        closeButton->setHoverIndex(361);
        closeButton->setIndex(360);
        Point tempVar5(180, 3);
        closeButton->setLocation(&tempVar5);
        closeButton->setLibrary(Libraries::Prguse2);
        closeButton->setParent(this);
        closeButton->setPressedIndex(362);
        closeButton->setSound(SoundList::ButtonA);
        closeButton->Click += [&] (sender, args)
        {
            CancelItemRental();
        };

        _lockButton->Click += [&] (o, e)
        {
            if (GameScene::getUser()->RentalGoldAmount < 1)
            {
                return;
            }
    
            C::ItemRentalLockFee tempVar6();
            Network::Enqueue(&tempVar6);
        };

        _rentalPriceButton = new MirButton();
        _rentalPriceButton->setIndex(28);
        Point tempVar7(18, 46);
        _rentalPriceButton->setLocation(&tempVar7);
        Size tempVar8(32, 17);
        _rentalPriceButton->setSize(&tempVar8);
        _rentalPriceButton->setLibrary(Libraries::Prguse);
        _rentalPriceButton->setParent(this);
        _rentalPriceButton->setSound(SoundList::Gold);
        _rentalPriceButton->Click += [&] (o, e)
        {
            if (GameScene::SelectedCell != nullptr || GameScene::Gold <= 0)
            {
                return;
            }
    
            auto amountBox = new MirAmountBox("Rental fee:", 116, GameScene::Gold);
    
            amountBox->OKButton->Click += [&] (c, a)
            {
                    if (amountBox->Amount <= 0)
                    {
                        return;
                    }

                    GameScene::getUser()->RentalGoldAmount += amountBox->Amount;
                    C::ItemRentalFee *tempVar9 = new C::ItemRentalFee();
                    tempVar9->Amount = GameScene::getUser()->RentalGoldAmount;
                    Network::Enqueue(tempVar9);

                    RefreshInterface();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar9' statement was not added since tempVar9 was passed to a method or constructor. Handle memory management manually.
            };
    
            amountBox->Show();
            GameScene::PickedUpGold = false;

            delete amountBox;
        };

        _nameLabel = new MirLabel();
        _nameLabel->setParent(this);
        Point tempVar10(30, 8);
        _nameLabel->setLocation(&tempVar10);
        Size tempVar11(150, 14);
        _nameLabel->setSize(&tempVar11);
        _nameLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        _nameLabel->setNotControl(true);

        _rentalPriceLabel = new MirLabel();
        _rentalPriceLabel->setParent(this);
        Point tempVar12(60, 42);
        _rentalPriceLabel->setLocation(&tempVar12);
        Size tempVar13(150, 14);
        _rentalPriceLabel->setSize(&tempVar13);
        _rentalPriceLabel->setDrawFormat(TextFormatFlags->Left | TextFormatFlags::VerticalCenter);
        _rentalPriceLabel->Click += [&] (o, e)
        {
            auto clickEventArgs = dynamic_cast<MouseEventArgs*>(e);
    
            if (clickEventArgs == nullptr)
            {
                return;
            }
    
            switch (clickEventArgs->Button)
            {
                case MouseButtons->Left:
                {
                    if (GameScene::SelectedCell != nullptr || GameScene::Gold <= 0)
                    {
                        return;
                    }
    
                    auto amountBox = new MirAmountBox("Rental fee:", 116, GameScene::Gold);
    
                    amountBox->OKButton->Click += [&] (c, a)
                    {
                            if (amountBox->Amount <= 0)
                            {
                                return;
                            }

                            GameScene::getUser()->RentalGoldAmount += amountBox->Amount;
                            C::ItemRentalFee *tempVar14 = new C::ItemRentalFee();
                            tempVar14->Amount = GameScene::getUser()->RentalGoldAmount;
                            Network::Enqueue(tempVar14);

                            RefreshInterface();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar14' statement was not added since tempVar14 was passed to a method or constructor. Handle memory management manually.
                    };
    
                    amountBox->Show();
                    GameScene::PickedUpGold = false;
    
                    break;

            delete amountBox;
                }
            }
        };

        delete closeButton;
        }

    void ItemRentDialog::RefreshInterface()
    {
        _nameLabel->setText(GameScene::getUser()->Name);
        _rentalPriceLabel->setText(std::string::Format("Rental Fee: {0,###,##0}", GameScene::getUser()->RentalGoldAmount:###));

        GameScene::Scene->GuestItemRentingDialog->RefreshInterface();
        GameScene::Scene->GuestItemRentDialog->RefreshInterface();

        Redraw();
    }

    void ItemRentDialog::OpenItemRentDialog()
    {
        GameScene::Scene->InventoryDialog->Show();

        Show();
        RefreshInterface();
        GameScene::Scene->GuestItemRentingDialog->Show();
    }

    void ItemRentDialog::Reset()
    {
        GameScene::getUser()->RentalGoldAmount = 0;
        GameScene::Scene->GuestItemRentDialog->Reset();

        RefreshInterface();
        Unlock();
        Hide();
    }

    void ItemRentDialog::Lock()
    {
        _lockButton->setIndex(253);
        _lockButton->setEnabled(false);
        _rentalPriceButton->setEnabled(false);

        RefreshInterface();
    }

    void ItemRentDialog::Unlock()
    {
        _lockButton->setIndex(250);
        _lockButton->setEnabled(true);
        _rentalPriceButton->setEnabled(true);
    }

    void ItemRentDialog::CancelItemRental()
    {
        C::CancelItemRental tempVar();
        Network::Enqueue(&tempVar);
    }

    std::string GuestItemRentDialog::getGuestName() const
    {
        return _guestName;
    }

    GuestItemRentDialog::GuestItemRentDialog() : _nameLabel(new MirLabel();
        _nameLabel->setParent(this);
        Point tempVar(30, 8);
        _nameLabel->setLocation(&tempVar);
        Size tempVar2(150, 14);
        _nameLabel->setSize(&tempVar2);
        _nameLabel->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        _nameLabel->setNotControl(true)), _rentalPriceLabel(new MirLabel();
        _rentalPriceLabel->setParent(this);
        Point tempVar3(60, 42);
        _rentalPriceLabel->setLocation(&tempVar3);
        Size tempVar4(150, 14);
        _rentalPriceLabel->setSize(&tempVar4);
        _rentalPriceLabel->setDrawFormat(TextFormatFlags->Left | TextFormatFlags::VerticalCenter);
        _rentalPriceLabel->setNotControl(true)), _lockButton(new MirButton();
        _lockButton->setIndex(250);
        Point tempVar5(22, 76);
        _lockButton->setLocation(&tempVar5);
        Size tempVar6(28, 25);
        _lockButton->setSize(&tempVar6);
        _lockButton->setLibrary(Libraries::Prguse);
        _lockButton->setParent(this);
        _lockButton->setEnabled(false)), _rentalPriceButton(new MirButton();
        _rentalPriceButton->setIndex(28);
        Point tempVar7(18, 46);
        _rentalPriceButton->setLocation(&tempVar7);
        Size tempVar8(32, 17);
        _rentalPriceButton->setSize(&tempVar8);
        _rentalPriceButton->setLibrary(Libraries::Prguse);
        _rentalPriceButton->setParent(this);
        _rentalPriceButton->setEnabled(false))
        {
        setIndex(238);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        Size tempVar9(204, 109);
        setSize(&tempVar9);
        Point tempVar10(Settings::ScreenWidth - getSize()->Width - getSize()->Width / 2, getSize()->Height + getSize()->Height / 2);
        setLocation(&tempVar10);
        setSort(true);




        }

    void GuestItemRentDialog::RefreshInterface()
    {
        _nameLabel->setText(_guestName);
        _rentalPriceLabel->setText(std::string::Format("Rental Fee: {0,###,##0}", _guestGold:###));

        Redraw();
    }

    void GuestItemRentDialog::SetGuestName(const std::string &name)
    {
        _guestName = name;
    }

    void GuestItemRentDialog::SetGuestFee(unsigned int amount)
    {
        _guestGold = amount;
    }

    void GuestItemRentDialog::Reset()
    {
        Unlock();
        _guestName = "";
        _guestGold = 0;

        Hide();
    }

    void GuestItemRentDialog::Lock()
    {
        _lockButton->setIndex(253);
        _guestGoldLocked = true;

        RefreshInterface();
    }

    void GuestItemRentDialog::Unlock()
    {
        _lockButton->setIndex(250);
        _guestGoldLocked = false;
    }
}
